package com.zxn.rv.utils

import android.animation.Animator
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.view.View
import android.view.ViewAnimationUtils
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.annotation.RequiresApi

/**
 * @author: njb
 * @date: 2020/2/23 0023 20:48
 * @desc:  揭露动画工具类
 */
object ViewCenterUtils {
    private var mX = 0
    private var mY = 0

    //得到视图中心
    @JvmStatic
    fun getViewCenter(view: View): IntArray {
        val top = view.top
        val left = view.left
        val bottom = view.bottom
        val right = view.right
        val x1 = (right - left) / 2
        val y1 = (bottom - top) / 2
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val x2 = location[0]
        val y2 = location[1]
        val x = x2 + x1
        return intArrayOf(x, y2)
    }

    //设置开始动画
    @JvmStatic
    fun setActivityStartAnim(activity: Activity, view: View?, intent: Intent) {
        view!!.post {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mX = intent.getIntExtra("x", 0)
                mY = intent.getIntExtra("y", 0)
                //对控件View进行判空，防止后台时间过长activity被回收后启动
                val animator = createRevealAnimator(activity, view, false, mX, mY)
                animator.start()
            }
        }
    }

    /**
     * 法让您能够为裁剪区域添加动画以揭露或隐藏视图
     *  参数主要是涉及到了动画开始的x，y坐标，以及圆形揭露的半径变化startRadius->endRadius。
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private fun createRevealAnimator(activity: Activity, view: View, reversed: Boolean, x: Int, y: Int): Animator {
        var a = x
        var b = y
        val measuredHeight = view.measuredHeight
        val screenWidth = activity.windowManager.defaultDisplay.width
        val screenHeight = activity.windowManager.defaultDisplay.height
        if (screenWidth - x > x) {
            a = screenWidth - x
        }
        if (screenHeight - y > y) {
            b = screenHeight - y
        }
        val hypot = Math.hypot(a.toDouble(), b.toDouble()).toFloat()
        val startRadius: Float = if (reversed) hypot else 0F
        val endRadius: Float = if (reversed) 0F else hypot
        val animator = ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius)
        animator.duration = 800
        animator.interpolator = AccelerateDecelerateInterpolator()
        if (reversed) {
            animator.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {}
                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.INVISIBLE
                    activity.finish()
                }

                override fun onAnimationCancel(animation: Animator) {}
                override fun onAnimationRepeat(animation: Animator) {}
            })
        }
        return animator
    }
}